<?php

class Client_Table_Groups extends Apk_Db_Table
{
	protected $_name      = 'groups';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)   
    		  ->where($this->_name.'.is_active')
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');    	
    		  
		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.add_date desc');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }
    

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where($this->_name.'.is_active')
					->where($this->_name.'.is_ghost = 0')
    		  		->where($this->_name.'.is_removed = 0')
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }    
    
    public function GetMemDataList($gid, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('groups_members', array('id' => 'id_acc', 'path', 'acc_name', 'is_added' => 'id_acc'))
    		  ->where('id_group = ?', $gid)
    		  ->order('acc_name');    		  

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }    
    
    public function GetNewsDataList($gid, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles', array('id', 'title', 'public_date', 'desc', 'add_user_id'))   
    		  ->join('accounts', 'accounts.id = articles.add_user_id', array('id', 'username', 'path', 'file_name'))
    		  ->where('articles.is_ghost = 0')    		  
    		  ->where('articles.is_removed = 0')
    		  ->where('articles.id_cat = 100')
    		  ->where('articles.id_group = ?', $gid);    	
    		  
		if(!is_null($sort) && strlen($sort) > 0) $sel->order('articles.'.$sort);
		else $sel->order('articles.public_date desc');
    		  
    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }
    
    public function GetNews($gid, $limit = 5)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles', array('id', 'title', 'public_date', 'comments_count', 'desc'))   
    		  ->join('accounts', 'accounts.id = articles.add_user_id', array('username'))
    		  ->where('articles.is_ghost = 0')    		  
    		  ->where('articles.is_removed = 0')
    		  ->where('articles.is_active')    		  
    		  ->where('id_group = ?', $gid)  
    		  ->order('articles.public_date desc')
    		  ->limit($limit);  	
    		  
    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }    
    
    public function GetMembers($id, $params = array(), $limit = 4)
    {
    	$sel = $this->getAdapter()->select()
    				->from('groups_members', array('id_group'))
    				->join('accounts', 'accounts.id = groups_members.id_acc', array('id', 'username', 'path', 'file_name'))
    				->where('groups_members.id_group = ?', $id)
    				->where('groups_members.is_accepted')
    				->order(array('groups_members.add_date desc'))
    				->limit($limit);  
    				
    	if(count($params) > 0)
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		default:
    			$sel->where('groups_members.'.$k.' = ?', $v);
    	}

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    } 
    
   /* public function GetMemDataList($gid, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('groups_members', array('id' => 'id_acc', 'path', 'acc_name', 'is_added' => 'id_acc', 'is_moderator', 'is_maccepted', 'is_editor'))
    		  ->where('id_group = ?', $gid)
    		  ->order(array('is_maccepted', 'add_date desc'));    		  

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }    */
    
    public function GetGroupsByUserID($id, $params = null)
    {
    	$sel = $this->getAdapter()->select()
    				->from('groups_members', array(''))
    				->join('groups', 'groups.id = groups_members.id_group')    				
    				->where('groups_members.id_acc = ?', $id)
    				->where('groups_members.is_maccepted');
    				
    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where('groups_members.'.$param.' = ?', $val);
    				break;
    		}
    	}    	

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();    	
    }
    
    public function GetCountGroupsByUserID($id, $params = null)
    {
    	$sel = $this->getAdapter()->select()
    				->from('groups_members', array('count' => 'count(id_acc)'))    				
    				->where('groups_members.id_acc = ?', $id)
    				->where('groups_members.is_maccepted')
    				->group('id_acc');
    				
    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where('groups_members.'.$param.' = ?', $val);
    				break;
    		}
    	}  
    	
//    	echo $sel->assemble(); die();

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();    	
    	return intval(@$data[0]['count']);
    }    
    
    public function GroupsUpdateMemCount($id)
    {
    	$sel = $this->getAdapter()->select()
					->from('groups_members', array("count" =>"count(id_group)"))
					->where('groups_members.is_maccepted')
					->where('groups_members.is_accepted')
					->where('id_group = ?', $id)
					->group('id_group');
					
		$data = $this->getList($sel)->getRow(0)->toArray();
		
		$d['count_accounts'] = $data['count'];
		
		$this->getAdapter()->update('groups', $d, 'id = '.$id);
    }
    
    public function GetMemList($id, $limit = null)
    {
    	$sel = $this->getAdapter()->select()
					->from('groups_members')					
					->where('groups_members.is_accepted')
					->where('groups_members.is_maccepted')
					->where('groups_members.id_group = ?', $id)
					->order('groups_members.add_date desc');

		if(!is_null($limit)) $sel->limit($limit);
					
		return $this->getList($sel)->toArray();		
    }
    
    //***********************************************   

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;   	    	    	
    	
		return parent::update($data, 'id = '.$id);
    }
    
	public function join_group($id_acc, $id_group)
    {    	    	
    	$sel = $this->getAdapter()->select()
    				->from('accounts')
    				->where('id = ?', $id_acc);
    	$user =  $this->getList($sel)->getRow(0)->toArray();    	
    	
    	$data['add_date'] = time();
    	$data['id_acc'] = $id_acc;
    	$data['id_group'] = $id_group;
    	$data['add_user_id'] = $id_acc;    	
    	$data['path'] = $user['path'];
    	$data['acc_name'] = $user['username'];    	
    	$data['is_accepted'] = 1;  
    	
    	try{    	
        	$this->getAdapter()->insert('groups_members', $data);
        	return true;
    	}catch(Exception $e){
    		$this->_ERROR = 'Заявка на включение вас в группу уже отправлена';
    		return false;
    	}
    }      
 

    public function accept_group($id_acc, $id_group)
    {
    	$d['is_accepted'] = 1;    	
    	$this->getAdapter()->update('groups_members', $d, 'id_group = '.$id_group.' and id_acc = '.$id_acc);		    	
    	$this->GroupsUpdateMemCount($id_group);
    }	
    
    public function leave_group($id_acc, $id_group)
    {    	
    	$this->getAdapter()->delete('groups_members', 'id_group = '.$id_group.' and id_acc = '.$id_acc);		    	
    	$this->GroupsUpdateMemCount($id_group);
    }   
    
    public function in_group($id_acc, $id_group)
    {    	
    	$sel = $this->getAdapter()->select()
    					->from('groups_members')
    					->where('id_group = ?', $id_group)
    					->where('id_acc = ?', $id_acc)
    					->where('is_maccepted')
    					->where('is_accepted ');     					
    	
   		return $this->getList($sel)->count();    					
    } 
    
        

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>